using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 事件接口
/// </summary>
public interface IEventInfo
{

}
public class EventInfo<T> : IEventInfo
{//带参数
    public UnityAction<T> actions;
    public EventInfo(UnityAction<T> action)
    {
        actions += action;
    }
}
public class EventInfo : IEventInfo
{//不带参数
    public UnityAction actions;
    public EventInfo(UnityAction action)
    {
        actions += action;
    }
}
/// <summary>
/// 事件中心
/// 1.事件字典
/// 2.委托
/// 3.观察者设计模式
/// 4.泛型
/// </summary>
public class EventCenter : Singleton<EventCenter>
{
    /// <summary>
    /// 事件字典
    /// key-- 事件的名字(比如：通过等等)
    /// value-- 对应的是 监听这个事件 对应的委托函数们
    /// </summary>
    private Dictionary<string, IEventInfo> eventDic = new Dictionary<string, IEventInfo>();
    /// <summary>
    /// 添加需要参数的监听
    /// </summary>
    /// <param name="eventName">事件名字</param>
    /// <param name="action">准备用来处理事件的委托函数</param>
    public void AddEvent<T>(string eventName, UnityAction<T> action)
    {
        //判断是否有对应的事件监听
        if (eventDic.ContainsKey(eventName))
        {
            (eventDic[eventName] as EventInfo<T>).actions += action;
        }
        else
        {
            eventDic.Add(eventName, new EventInfo<T>(action));
        }
    }
    /// <summary>
    /// 添加无参数的监听
    /// </summary>
    /// <param name="eventName">事件的名字</param>
    /// <param name="action">准备用来处理事件的委托函数</param>
    public void AddEvent(string eventName,UnityAction action)
    {
        //判断是否有对应的事件监听
        if (eventDic.ContainsKey(eventName))
        {
            (eventDic[eventName] as EventInfo).actions += action;
        }
        else
        {
            eventDic.Add(eventName, new EventInfo(action));
        }
    }
    /// <summary>
    /// 移除有参数的监听
    /// </summary>
    /// <typeparam name="T">参数类型</typeparam>
    /// <param name="eventName">事件名字</param>
    /// <param name="action">对应字典里的事件</param>
    public void RemoveEvent<T>(string eventName, UnityAction<T> action)
    {
        //判断是否有对应的事件监听
        if (eventDic.ContainsKey(eventName))
            (eventDic[eventName] as EventInfo<T>).actions -= action;
    }
    /// <summary>
    /// 移除无参数的监听
    /// </summary>
    /// <param name="eventName">事件名字</param>
    /// <param name="action">对应字典里的事件</param>
    public void RemoveEvent(string eventName, UnityAction action)
    {
        //判断是否有对应的事件监听
        if (eventDic.ContainsKey(eventName))
            (eventDic[eventName] as EventInfo).actions -= action;
    }
    /// <summary>
    /// 事件触发(有参数)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="eventName"></param>
    /// <param name="info"></param>
    public void EventTrigger<T>(string eventName,T info)
    {
        //判断是否有对应的事件监听
        if(eventDic.ContainsKey(eventName))
        {
            if ((eventDic[eventName] as EventInfo<T>).actions != null)
                (eventDic[eventName] as EventInfo<T>).actions.Invoke(info);
        }
    }
    /// <summary>
    /// 事件触发(无参数)
    /// </summary>
    /// <param name="eventName"></param>
    public void EventTrigger(string eventName)
    {
        //判断是否有对应的事件监听
        if (eventDic.ContainsKey(eventName))
        {
            if ((eventDic[eventName] as EventInfo).actions != null)
                (eventDic[eventName] as EventInfo).actions.Invoke();
        }
    }
    /// <summary>
    /// 清空事件中心
    /// </summary>
    public void Clear()
    {
        eventDic.Clear();
    }
}
